package com.cola;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @Grup:com.hxh
 * @Auther:cola
 * @Data:2021/3/9-2021-03-16:23
 * @version:1.0
 */
public class CompressionAndcompressionDemo {
    /*
    * 压缩
    * */
    private static void compression(String zipFileName, File targetFile){
        System.out.println("正在压缩...");
        try {
            //要生成的压缩文件
            ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
            BufferedOutputStream bos=new BufferedOutputStream(out);
            zip(out,targetFile,targetFile.getName(),bos);
            bos.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //zip
    private static void zip(ZipOutputStream Zout, File targetFile, String name, BufferedOutputStream bos) throws IOException {
        //如果是个目录
        if (targetFile.isDirectory()){
            File[]files=targetFile.listFiles();
            if (files.length==0){
                Zout.putNextEntry(new ZipEntry(name+"/"));//处理空目录
            }
            for (File f:files){
                zip(Zout,f,name+"/"+f.getName(),bos);
            }
        }else {
            Zout.putNextEntry(new ZipEntry(name));
            InputStream in=new FileInputStream(targetFile);
            BufferedInputStream bis=new BufferedInputStream(in);
            byte[]bytes=new byte[1024];
            int len=-1;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
            bis.close();
        }
        System.out.println("压缩完成!!!");
    }

    /*
    * 解压
    * */
    private static void decompression(String targeFileName,String parent){
        try {
            //构造解压的输入流
            ZipInputStream zIn=new ZipInputStream(new FileInputStream(targeFileName));
            ZipEntry entry=null;
            File file=null;
            while ((entry=zIn.getNextEntry())!=null&& !entry.isDirectory()){
                file=new File(parent,entry.getName());
                if (!file.exists()){
                    new File(file.getParent()).mkdirs();//创建此文件的上级目录
                }
                FileOutputStream out =new FileOutputStream(file);
                BufferedOutputStream bos=new BufferedOutputStream(out);
                byte[] bytes=new byte[1024];
                int len=-1;
                while ((len=zIn.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                }
                bos.close();
                System.out.println(file.getAbsolutePath()+"解压成功!");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        compression("FileAndIO/src/main/newFile/information",new File("FileAndIO/src/main/newFile/information.zip"));
        decompression("FileAndIO_08/src/main/newFile/information.zip","D:\\Program Files (x86)\\test\\");
    }
}
